package com.songcha.module_bookdetail.ui.activity.catalog

import android.os.Bundle
import android.view.View
import android.view.ViewGroup

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.songcha.library_base.mvvm.base.BaseMvvmActivity
import com.songcha.library_business.bean.book.BookBean
import com.songcha.library_business.bean.book.BookChapterCatalogBean
import com.songcha.library_business.config.FunctionConfig
import com.songcha.library_business.provider.IBookDownloadProvider
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_business.util.BusinessJumpUtil
import com.songcha.library_common.util.StatusBarUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.module_bookdetail.R
import com.songcha.module_bookdetail.databinding.BookdetailActivityCatalogBinding
import com.songcha.module_bookdetail.ui.adapter.BookCatalogAdapter

class BookCatalogActivity:BaseMvvmActivity<BookCatalogViewModel,BookdetailActivityCatalogBinding>() ,com.chad.library.adapter.base.listener.OnItemClickListener,View.OnClickListener{
    private var mAdapter:BookCatalogAdapter?=null

    override fun getLayoutId(): Int {
        return R.layout.bookdetail_activity_catalog
    }

    override fun isNeedShowLoad(): Boolean {
        return true
    }

    override fun isCalculateHearBarHeight(): Boolean {
        return true
    }

    override fun isImmerseStatusBar(): Boolean {
        return true
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        databinding.bookdetailLlHeaderbar.layoutParams=databinding.bookdetailLlHeaderbar.layoutParams.apply {
            this as ViewGroup.MarginLayoutParams
            topMargin=StatusBarUtil.getStatusBarHeight(this@BookCatalogActivity)
        }

        intent?.let {
            viewModel.bookDetail=it.getParcelableExtra("book_detail")?:return
        }


        if(viewModel.bookDetail==null){
            ToastUtil.show("无法获取书籍目录")
            return
        }

        if(FunctionConfig.IS_VIP_ENABLED){
            if(UserInfoManager.isVip()){
                databinding.bookdetailIvDownload.visibility=View.VISIBLE
            }else{
                databinding.bookdetailIvDownload.visibility=View.GONE
            }
            viewModel.isBookAllDownload.observe(this){data->
                if(data){
                    databinding.bookdetailIvDownload.visibility=View.GONE
                }else{
                    if(UserInfoManager.isVip())
                        databinding.bookdetailIvDownload.visibility=View.VISIBLE
                }
            }

        }else{
            databinding.bookdetailIvDownload.visibility=View.GONE
        }


        viewModel.getIsBookInShelf()

        databinding.bookdetailIvOrder.setOnClickListener {
            viewModel.chapterList.value!!.reverse()
            viewModel.isReverse=!viewModel.isReverse
            mAdapter?.notifyDataSetChanged()
            databinding.bookdetailRcvCatalog.scrollToPosition(0)
        }


        databinding.bookdetailIvBack.setOnClickListener(this)
        databinding.bookdetailIvAddtoShelf.setOnClickListener(this)
        databinding.bookdetailIvDownload.setOnClickListener(this)


        viewModel.chapterList.observe(this){data->
            initRcv(data)
            viewModel.getHasBookAllDownload()
        }


        viewModel.isInBookShelf.observe(this){data->
            if(data){
                databinding.bookdetailIvAddtoShelf.visibility=View.GONE
            }else{
                databinding.bookdetailIvAddtoShelf.visibility=View.VISIBLE
            }
        }

    }

    override fun onGetData() {
        if(viewModel.bookDetail==null) return
        viewModel.getBookChapterCatalog()
    }

    private fun initRcv(list:MutableList<BookChapterCatalogBean.DataBean>){
        databinding.bookdetailRcvCatalog.apply{
            layoutManager=LinearLayoutManager(context)
            mAdapter=BookCatalogAdapter(list)
            mAdapter!!.setOnItemClickListener(this@BookCatalogActivity)
            adapter=mAdapter
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        if(viewModel.chapterList.value==null) return
        var chapter=position+1
        if(viewModel.isReverse){
            chapter=viewModel.chapterList.value!!.size-position
        }

        //val bean=viewModel.chapterList.value!![chapter-1]

        val book= BookBean()
        book.id=viewModel.bookDetail!!.bookId.toString()
        book.bookName=viewModel.bookDetail!!.bookName
        book.thumb=viewModel.bookDetail!!.cover
        book.textCount=viewModel.bookDetail!!.words
        book.author=viewModel.bookDetail!!.author

        BusinessJumpUtil.toBookReader(book,chapter)
    }

    override fun onClick(v: View) {
        when(v.id){
            R.id.bookdetail_iv_back->{
                finish()
            }
            R.id.bookdetail_iv_addto_shelf->{
                viewModel.addBookToShelf()
                ToastUtil.show("已加入书架")
            }
            R.id.bookdetail_iv_download->{
                //下载的同时加入书架
                viewModel.addBookToShelf()
                ARouter.getInstance().build("/bookreader/BookDownProviderImpl").navigation()?.let {
                    val book=BookBean()
                    viewModel.bookDetail?.let {
                        book.id=it.bookId.toString()
                        book.bookName=it.bookName
                        book.author=it.author
                        book.thumb=it.cover
                        book.textCount=it.words
                    }
                    (it as IBookDownloadProvider).startDownload(book,viewModel.chapterList.value!!)
                }
                //BookDownloadHelper.startBookDownloadService(viewModel.book,viewModel.chapterList)
            }
        }
    }
}